package com.ruoyi.xxljob.admin.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.xxljob.admin.core.model.XxlJobInfo;
import com.ruoyi.xxljob.admin.core.model.XxlJobLogGlue;
import com.ruoyi.xxljob.admin.core.util.I18nUtil;
import com.ruoyi.xxljob.admin.dao.XxlJobInfoDao;
import com.ruoyi.xxljob.admin.dao.XxlJobLogGlueDao;
import com.ruoyi.xxljob.core.glue.GlueTypeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * job code controller
 * glue IDE 相关
 *
 * @author wjialong
 */
@RestController
@RequestMapping("/code")
public class JobCodeController {

    @Resource
    private XxlJobInfoDao xxlJobInfoDao;
    @Resource
    private XxlJobLogGlueDao xxlJobLogGlueDao;

    /**
     * 获取该任务IDE源码的各个历史版本
     *
     * @param request
     * @param jobId
     * @return
     */
    @GetMapping()
    public AjaxResult index(HttpServletRequest request, int jobId) {
        XxlJobInfo jobInfo = xxlJobInfoDao.loadById(jobId);
        List<XxlJobLogGlue> jobLogGlues = xxlJobLogGlueDao.findByJobId(jobId);

        if (jobInfo == null) {
            throw new RuntimeException(I18nUtil.getString("jobinfo_glue_jobid_unvalid"));
        }
        if (GlueTypeEnum.BEAN == GlueTypeEnum.match(jobInfo.getGlueType())) {
            throw new RuntimeException(I18nUtil.getString("jobinfo_glue_gluetype_unvalid"));
        }

//        // valid permission
//        JobInfoController.validPermission(request, jobInfo.getJobGroup());

        // Glue类型-字典
//        model.addAttribute("GlueTypeEnum", GlueTypeEnum.values());

//        model.addAttribute("jobInfo", jobInfo);
//        model.addAttribute("jobLogGlues", jobLogGlues);

//        return "jobcode/jobcode.index";
        return AjaxResult.success(jobLogGlues);
    }

    /**
     * 保存IDE源码
     *
     * @param id
     * @param glueSource
     * @param glueRemark
     * @return
     */
    @PutMapping()
    public AjaxResult save(@RequestBody XxlJobLogGlue jobLogGlue) {
        // valid
        if (jobLogGlue.getGlueRemark() == null) {
            return AjaxResult.error(500, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_glue_remark")));
        }
        if (jobLogGlue.getGlueRemark().length() < 4 || jobLogGlue.getGlueRemark().length() > 100) {
            return AjaxResult.error(500, I18nUtil.getString("jobinfo_glue_remark_limit"));
        }
        XxlJobInfo exists_jobInfo = xxlJobInfoDao.loadById(jobLogGlue.getJobId());
        if (exists_jobInfo == null) {
            return AjaxResult.error(500, I18nUtil.getString("jobinfo_glue_jobid_unvalid"));
        }

        // update new code
        exists_jobInfo.setGlueSource(jobLogGlue.getGlueSource());
        exists_jobInfo.setGlueRemark(jobLogGlue.getGlueRemark());
        exists_jobInfo.setGlueUpdatetime(new Date());

        exists_jobInfo.setUpdateTime(new Date());
        xxlJobInfoDao.update(exists_jobInfo);

        // log old code
        XxlJobLogGlue xxlJobLogGlue = new XxlJobLogGlue();
        xxlJobLogGlue.setJobId(exists_jobInfo.getId());
        xxlJobLogGlue.setGlueType(exists_jobInfo.getGlueType());
        xxlJobLogGlue.setGlueSource(jobLogGlue.getGlueSource());
        xxlJobLogGlue.setGlueRemark(jobLogGlue.getGlueRemark());

        xxlJobLogGlue.setAddTime(new Date());
        xxlJobLogGlue.setUpdateTime(new Date());
        xxlJobLogGlueDao.save(xxlJobLogGlue);

        // remove code backup more than 30
        xxlJobLogGlueDao.removeOld(exists_jobInfo.getId(), 30);

        return AjaxResult.success();
    }
}
